from sklearn.datasets import fetch_olivetti_faces
from numpy.random import RandomState
import matplotlib.pyplot as plt

dataset = fetch_olivetti_faces(shuffle=True, random_state=RandomState(0))
faces = dataset.data
print(faces.shape)

n_row, n_col = 5, 12; n_images = n_row * n_col

def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray):
    fig, axs = plt.subplots(n_row,n_col, figsize=(18,7),
                            subplot_kw={'xticks':(), 'yticks':()})
    for i, image in zip(range(n_images), images):
        r = int(i/n_col); c = i%n_col
        axs[r,c].imshow(image.reshape((64, 64)), cmap=cmap)

plot_gallery("Olivetti faces", faces[:n_components])

plt.show()